import 'video.js/dist/video-js.css'
import videojs from 'video.js/dist/video'
import extToFileType from '../extToFileType.js'

window.videojs = videojs

require('videojs-contrib-hls/dist/videojs-contrib-hls.js')

export default {
  data () {
    return {
      player: null,
      isTransing: false,
      transcodingStatus: 'loading',
      extention: '' // 最终文件后缀
    }
  },

  computed: {
    // 文件扩展类型
    type () {
      return extToFileType(`*.${this.extention}`)
    },

    // 信息提示
    msg () {
      if (this.transcodingStatus === 'loading') {
        return '数据加载中...'
      } else if (this.transcodingStatus === 'wating') {
        return '转码中，点击刷新状态...'
      } else {
        return '转码未完成，请稍候重试...'
      }
    }
  },

  methods: {
    init () {
      // 最终扩展名
      this.extention = this.ext || this.videoUrl.url.substring(this.videoUrl.url.lastIndexOf('.') + 1)

      if (this.$options.name === 'tw-pop-player') {
        if (this.playerVisible) {
          this.pushState('pop-player-open')
        } else {
          return
        }
      }

      this.$nextTick(() => {
        if (this.isEcoTrnascodeing) {
          this.isTransing = true
          this.transcodingStatus = 'loading'
          this.getTranscodingPath()
        } else {
          this.initVideo(this.videoUrl.url)
        }
      })
    },

    initVideo (src) {
      const vedioMime = {
        mp4: 'video/mp4',
        ogg: 'video/ogg',
        webm: 'video/webm',
        m3u8: 'application/x-mpegURL'
      }

      if (!/^(http|htps)/.test(src)) {
        src = `${this.fileBaseUrl}${src}`
      }

      const source = {
        src,
        type: this.mimeType === 'auto' ? vedioMime[this.extention] : this.mimeType
      }

      if (this.player) {
        this.player.src(source)
        this.player.load()
        return
      }

      const options = {
        sources: [source]
      }

      Object.assign(options, this.options)
      this.player = videojs(this.$refs.player, options)

      // setTimeout(() => {
      //   this.player = videojs(this.$refs.player, options)
      // }, 1000)
    },

    getTranscodingPath () {
      const vm = this

      if (this.transcodingStatus === 'cooldown') {
        return
      }

      const getWayUrl = this.ecoGatewayUrl
      const shortSrc = this.videoUrl.url.substring(this.fileBaseUrl.length)

      // 如果为eco转码的，先检测转码是否完成，返回一个m3u8格式的后缀名媒体地址，即为转码完成
      this.$axios.get(`${getWayUrl}/fs/fs/file/getMediaConvertPath?${shortSrc}`).then((resp) => {
        if (resp.data.serverResult.resultCode === '200') {
          if (/\.m3u8$/.test(resp.data.responseEntity)) {
            vm.extention = 'm3u8'
            vm.initVideo(this.fileBaseUrl + resp.data.responseEntity)

            vm.isTransing = false
          } else {
            // 如果未转码，调用一次eco转码
            vm.$axios.post(`${getWayUrl}/fs/fs/media/convert`, [{
              filePath: resp.data.responseEntity
            }])
            vm.transcodingStatus = 'cooldown'
          }
        } else {
          vm.transcodingStatus = 'cooldown'
        }
      }).catch(() => {
        vm.transcodingStatus = 'cooldown'
      })
    },

    stop () {
      if (this.player) {
        this.player.currentTime(0)
        setTimeout(() => {
          this.player.pause()
        }, 0)
      }
    },

    destroyPlayer () {
      if (this.player) {
        this.player.dispose()
        this.player = null
      }
    }
  },

  mounted () {
    this.init()
  },

  beforeDestroy () {
    this.destroyPlayer()
  },

  watch: {
    src () {
      this.init()
    },

    transcodingStatus (val) {
      if (val === 'cooldown') {
        setTimeout(() => {
          this.transcodingStatus = 'wating'
        }, 3000)
      }
    }
  }
}
